package com.pajk.user.selftest;

import com.pajk.user.service.common.http.HttpsChannel;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.core.io.ClassPathResource;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by chenhaihua on 14-4-17.
 */
public class CreditPress {
    private static final String queryUrl = "https://bisfront.stg.wanlitong.com:8010/invoke/wm.tn/receive?";
    public static void main(String[] args) throws Exception {
        final HttpsChannel wltHttpsChannel = new HttpsChannel();
        wltHttpsChannel.setKeyStorePassword("22622689");
        wltHttpsChannel.setTrustStorePassword("123456");
        wltHttpsChannel.setKeyStoreType("pkcs12");
        wltHttpsChannel.setKeyStorePath("META-INF/wlt/keyStore_stg.pfx");
        wltHttpsChannel.setTrustStorePath("META-INF/wlt/trustStore_stg.jks");
        wltHttpsChannel.init();

        SAXReader reader = new SAXReader();
        ClassPathResource addCreditXml = new ClassPathResource("META-INF/wlt/addCredit.xml");
        ClassPathResource queryCreditXml = new ClassPathResource("META-INF/wlt/queryCredit.xml");
        ClassPathResource consumeCreditXml = new ClassPathResource("META-INF/wlt/consumeCredit.xml");

        Document addCreditDoc = reader.read(addCreditXml.getFile());
        Document queryCreditDoc = reader.read(queryCreditXml.getFile());
        Document consumeCreditDoc = reader.read(consumeCreditXml.getFile());
        Element memberIdElement = ((Element) queryCreditDoc.selectNodes(
            "/LPMS_Pionts/Request/PropertySet/SiebelMessage/MemberID").get(0));
        memberIdElement.setText("010000017311534");
        final String queryXml = queryCreditDoc.asXML();
        System.out.println(queryXml);
        for (int i = 0; i < 30; i++) {
            long begin = System.currentTimeMillis();
            Document result = wltHttpsChannel.call(
                    queryUrl, queryXml);
            System.out.println("creditQuery" + i + " cost:" + (System.currentTimeMillis() - begin)+"ms");
        }
        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(30);
        final AtomicInteger count = new AtomicInteger(0);
        for (int i = 0; i < 30; i++) {
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    long begin = System.currentTimeMillis();
                    Document result = wltHttpsChannel.call(
                            queryUrl, queryXml);
                    int countN = count.incrementAndGet();
                    System.out.println("creditQuery-THREAD" + countN + " cost:" + (System.currentTimeMillis() - begin)+"ms");
                }
            });
        }
        while (count.get() <30){
            Thread.sleep(2000);
        }
        /*count.set(0);
        for (int i = 0; i < 30; i++) {
            fixedThreadPool.execute(new Runnable() {
                @Override
                public void run() {
                    long begin = System.currentTimeMillis();
                    Document result = wltHttpsChannel.call(
                            "https://eairiis-stgdmz.paic.com.cn/invoke/wm.tn/receive?", queryXml);
                    int countN = count.incrementAndGet();
                    System.out.println("creditQuery-RTHREAD" + countN + " cost:" + (System.currentTimeMillis() - begin)+"ms");
                }
            });
        }
        while (count.get() <30){
            Thread.sleep(2000);
        }*/
    }
}
